package com.ldf.jrtsp.parser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static com.ldf.jrtsp.constants.RtspConstants.RTSP_DELIMITER;

/**
 * rtsp数据解析器器
 *
 * @author lidaofu
 * @since 2024/6/19
 **/
public class RtspDataParser {
    private final static Logger log = LoggerFactory.getLogger(RtspDataParser.class);
    private String response;
    private Map<String, String> headers = new HashMap<>();
    private String sdp;

    public RtspDataParser(byte[] data) {
        String dataStr = new String(data, StandardCharsets.UTF_8);
        log.debug("receive data:\n{}", dataStr);
        String[] headerArray = dataStr.split(RTSP_DELIMITER);
        for (String header : headerArray) {
            String[] contentStr = header.split(": ");
            if (contentStr.length == 1) {
                response = contentStr[0];
            } else if (contentStr.length == 2) {
                headers.put(contentStr[0], contentStr[1]);
            }
        }


    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public String getSdp() {
        return sdp;
    }

    public void setSdp(String sdp) {
        this.sdp = sdp;
    }
}
